package com.tianlu.store.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.tianlu.store.common.IdGen;
import com.tianlu.store.common.ResultCode;
import com.tianlu.store.common.ServiceException;
import com.tianlu.store.entity.OrderProduct;
import com.tianlu.store.entity.ProductInfo;
import com.tianlu.store.entity.ShoppingCar;

import com.tianlu.store.mapper.ShoppingCarMapper;
import com.tianlu.store.service.ProductInfoService;
import com.tianlu.store.service.ShoppingCarService;
import com.tianlu.store.utils.JwtUtils;
import org.springframework.stereotype.Service;
import com.tianlu.store.common.MyBaseServiceImpl;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author 梁东烂
 * @Desc 购物车表(ShoppingCar)表服务实现类
 * @Date 2023-04-27 14:07:33
 */
@Service("shoppingCarService")
public class ShoppingCarServiceImpl extends MyBaseServiceImpl<ShoppingCarMapper, ShoppingCar> implements ShoppingCarService {
    @Resource
    ProductInfoService productInfoService;

    /**
     * 查询购物车
     *
     * @param userId 用户id
     * @return List<OrderProduct>
     * @author liangdl
     * @since 2023/4/27 14:14
     */
    @Override
    public List<ProductInfo> findProductList(Long userId) {
        ShoppingCar query = new ShoppingCar();
        query.setUserId(userId);
        List<ShoppingCar> list = dao.findList(query);
        if (CollUtil.isEmpty(list)) {
            return new ArrayList<>(0);
        }

        Map<Long, Integer> quantityMap = list.stream().collect(Collectors.toMap(ShoppingCar::getProductId, ShoppingCar::getQuantity));

        List<Long> productIdList = list.stream().map(ShoppingCar::getProductId).collect(Collectors.toList());
        List<ProductInfo> productList = productInfoService.findByIdList(productIdList);

        for (ProductInfo productInfo : productList) {
            productInfo.setQuantity(quantityMap.get(productInfo.getId()));
        }

        return productList;
    }

    /**
     * 从购物车中删除
     *
     * @param productIdList 产品id
     * @param userId        用户id
     * @author liangdl
     * @since 2023/4/27 14:19
     */
    @Override
    public void deleteProduct(List<Long> productIdList, Long userId) {
        dao.deleteProduct(productIdList, userId);
    }

    /**
     * 修改购物车中商品数量
     *
     * @param productId 产品id
     * @param userId    用户id
     * @param quantity  数量
     * @author liangdl
     * @since 2023/4/27 14:19
     */
    @Override
    public void updateProductQuantity(Long productId, Integer quantity, Long userId) {
        if (quantity <= 0) {
            dao.deleteProduct(Collections.singletonList(productId), userId);
        } else {
            ProductInfo productInfo = productInfoService.getById(productId);
            if (Objects.isNull(productInfo)) {
                throw new ServiceException(ResultCode.CODE_400, "商品不存在！");
            }

            ShoppingCar query = new ShoppingCar();
            query.setUserId(userId);
            query.setProductId(productId);
            List<ShoppingCar> list = dao.findList(query);
            if (CollUtil.isEmpty(list)) {
                ShoppingCar shoppingCar = new ShoppingCar();
                shoppingCar.setId(IdGen.snowflakeId());
                shoppingCar.setUserId(userId);
                shoppingCar.setProductId(productId);
                shoppingCar.setQuantity(quantity);
                super.save(shoppingCar);
            } else {
                ShoppingCar shoppingCar = list.get(0);
                shoppingCar.setQuantity(quantity);
                super.updateById(shoppingCar);
            }
        }
    }

    /**
     * 清空购物车
     *
     * @param userId 用户id
     * @author liangdl
     * @since 2023/4/27 14:19
     */
    @Override
    public void cleanUp(Long userId) {
        dao.deleteByUserId(userId);
    }
}
